package com.InetAddressDemo;


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * 广播模式
 * @Author weijingchen
 * @Date 2025/6/23 22:16
 * @Version 1.0.0
 */
public class Test04 {
    //发送数据
    public static void main(String[] args) throws IOException {
        //创建DatagramSocket对象,空参构造端口随机
        DatagramSocket ds = new DatagramSocket();
        Scanner sc = new Scanner(System.in);
        //创建DatagramPacket对象,封装数据,封装端口号
        while (true) {
            System.out.println("请输入数据:");
            String s = sc.nextLine();
            byte[] bytes = s.getBytes();
            InetAddress address = InetAddress.getByName("wei");
            int port = 10086;
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);

            ds.send(dp);
            if ("bye".equalsIgnoreCase(s) || "886".equals(s)) {
                break;
            }
        }

        ds.close();
    }
}
